package com.tos.quran;

import java.io.File;

import com.bappi.utils.Utils;
import com.custom.tab.AbstractTabActivity;
import com.custom.tab.AbstractTabRootManager;
import com.quran.db.DatabaseAccessor;
import com.quran.db.DatabaseHelper;

import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.ViewAnimator;
import android.widget.ViewFlipper;

public class QuranActivity extends AbstractTabActivity implements
		OnSharedPreferenceChangeListener {

	private static final int[] tabHeaderIds = { R.id.tab_hearder_1,R.id.tab_hearder_2,R.id.tab_hearder_3,R.id.tab_hearder_4 };

	
	private DatabaseAccessor databaseAccessor;
	
	private ViewFlipper viewFlipper;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState, tabHeaderIds, R.layout.main,
				R.id.tab_header, R.id.tab_content,null, null, false);
		viewFlipper = (ViewFlipper)findViewById(R.id.main_view_flipper);
		Utils.createShortCut(this,QuranActivity.class);
		initilizeDatabase();
//		addTabs();
	}

	public AbstractTabRootManager getTabRootManager(
			LayoutInflater layoutInflater, int index, ViewAnimator child) {
		return new QuranTRM(this, layoutInflater, child, index);
	}
	
	@Override
	public File getDatabasePath(String name) {
		return getApplication().getDatabasePath(name);
	}
	
	public DatabaseAccessor getDatabaseAccessor() {
		return databaseAccessor;
	}
	
	////////////////////////////////////////////
	
	private void initilizeDatabase() {
		final int existingDatabaseFileVersion = DatabaseHelper
				.getExistingDatabaseFileVersion(QuranActivity.this);
		if (existingDatabaseFileVersion == DatabaseHelper.DB_VERSION) {
			initilizeDatabase(existingDatabaseFileVersion, false);
		} else if (existingDatabaseFileVersion >= DatabaseHelper.CURRENT_DATABASE_STRUCTURE_FIRST_VERSION) {
			initilizeDatabase(
					existingDatabaseFileVersion,
					true);
		} else {
			DatabaseHelper.deleteDatabaseFile(QuranActivity.this,
					existingDatabaseFileVersion);
			initilizeDatabase(existingDatabaseFileVersion, false);
		}
	}

	private void initilizeDatabase(final int existingDatabaseFileVersion,
			final boolean importUserData) {
		new AsyncTask<String, String, String>() {

			protected String doInBackground(String... arg0) {
				try {

					// boolean isBackedUp;

					try {
						

						DatabaseHelper.manageDatabase(QuranActivity.this,
								false);
						databaseAccessor = new DatabaseAccessor(
								QuranActivity.this);
					} catch (Exception e) {

						((QuranApp) getApplication()).setOnSdCard(false);

						// if(importUserData){
						// DatabaseHelper.backupInFile(QuranActivity.this,
						// existingDatabaseFileVersion);
						// }
						DatabaseHelper.manageDatabase(QuranActivity.this,
								false);
						databaseAccessor = new DatabaseAccessor(
								QuranActivity.this);
					}

					

				} catch (Exception e) {
				}
				return null;
			}

			protected void onPostExecute(String unused) {
				try {

//					final String[] TAB_TITLES = { getString(R.string.home),
//							getString(R.string.favorites),
//							getString(R.string.games),
//							getString(R.string.others) };
//					addTabs(ICONS, TAB_TITLES);
//
//					
//					handleAds();
					Log.i("DREG", "databaseAccessor isNull="+(databaseAccessor==null));
					addTabs();
					viewFlipper.setDisplayedChild(1);
				} catch (Exception e) {
				}
			}
		}.execute();
	}
}